import {createRouter, createWebHashHistory} from "vue-router";
import {getCookie} from '~/api/manager'
const routes = [
    {   name: 'Login',
        path: '/login',
        component: () => import('~/pages/login/index.vue'),
    }, {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: () => import('~/pages/404.vue'),
    },
    {   name: '布局',
        path: '/',
        component: () => import('~/layout/index.vue'),
        redirect: '/index',
        children:[
            {
                name:"首页",
                path: '/index',
                component: () => import('~/pages/index.vue'),
            },{
                name: '资料',
                path: '/profile',
                redirect: '/index',
                children:[
                    {   name: '仓库管理',
                        path: '/profile/warehouseManager',
                        component: () => import('~/pages/WarehouseManager/index.vue'),
                    },
                ]
            },

            {   name: '聊天',
                path: '/chat',
                component: () => import('~/components/Chat.vue'),
            },
        ]
    }
]


const router = createRouter({
    history: createWebHashHistory(),
    routes: routes
})


// 路由守卫
router.beforeEach((to, from, next) => {
    const token = getCookie('yuan-token'); // 从 cookie 中获取 token

    if (to.name !== 'Login' && !token) {
        // 如果用户未登录且试图访问非登录页面，重定向到登录页面
        next({ name: 'Login' });
    } else {
        next(); // 允许访问
    }
});
export default router;